package com.hcmu.view.common;

import vanhoc.android.utils.ResourceUtils;

import com.telpoo.truyencuoi.R;
import com.hcmu.common.SharePreferencesHelper;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.widget.TextView;

public class SettingFontSize {
	private static Context mContext;
	private static final int  FONT_SMALL_SIZE = 16;
	private static final int  FONT_NORMAL_SIZE = 18;
	private static final int FONT_LARGE_SIZE = 20;
	static SharedPreferences sharePreferences;
	
	public SettingFontSize(Context context,SharedPreferences sp) {
		mContext = context;
		sharePreferences = sp;
	}
	
	
	public static int getTextSize() {
		return sharePreferences.getInt("FONT_SIZE", FONT_SMALL_SIZE);
	}
	
	
	
	public static void show(final TextView view) {
		Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle(mContext.getResources().getString(R.string.CHOOSE_FONT_SIZE));
		
		//items
		final String[] items = mContext.getResources().getStringArray(R.array.setting_font_text);
		
		int itemSelected;
		//int fontSize = spHelper.getPreferences("FONT_SIZE", FONT_SMALL_SIZE);
		
		int fontSize = sharePreferences.getInt("FONT_SIZE", FONT_SMALL_SIZE);
		switch (fontSize) {
			case FONT_SMALL_SIZE:
				itemSelected = 0;
				break;
			case FONT_NORMAL_SIZE:
				itemSelected = 1;
				break;
			case FONT_LARGE_SIZE:
				itemSelected = 2;
				break;
			default:
				itemSelected = -1;
				break;
		}
		
		builder.setSingleChoiceItems(items, itemSelected, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
					case 0:
						saveSharePreferences("FONT_SIZE", FONT_SMALL_SIZE);
						break;
					case 1:
						saveSharePreferences("FONT_SIZE", FONT_NORMAL_SIZE);
						break;
					case 2:
						saveSharePreferences("FONT_SIZE", FONT_LARGE_SIZE);
						break;
					default:
						break;
				}
				
				view.setTextSize(getTextSize());
				dialog.dismiss();
			}
		});
		
		builder.setNegativeButton(mContext.getResources().getString(R.string.CANCEL), new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		
		AlertDialog alertBox = builder.create();
		alertBox.show();
	}
	
	
	public static void saveSharePreferences(String key,int value){
		SharedPreferences.Editor editor = sharePreferences.edit();
		editor.putInt(key, value);
		editor.commit();
	}
	
}
